<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <template #title>
        <div class="title-text">我的频道</div>
      </template>
      <van-button
        class="edit-btn"
        type="danger"
        plain
        round
        size="mini"
        @click="isEdit = !isEdit"
        >{{ isEdit ? '完成' : '编辑' }}</van-button
      >
    </van-cell>
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(v, i) in myChannels"
        :key="i"
        @click="onMyChannelClick(v, i)"
      >
        <template #icon>
          <van-icon
            name="clear"
            v-show="isEdit && !fiexChannels.includes(v.id)"
          ></van-icon>
        </template>
        <template #text>
          <span class="text" :class="{ active: i === active }">{{
            v.name
          }}</span>
        </template>
      </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell :border="false">
      <template #title>
        <div class="title-text">频道推荐</div>
      </template>
    </van-cell>
    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(v, i) in recommendChannels"
        :key="i"
        :text="v.name"
        icon="plus"
        @click="onAdddChannel(v)"
      />
    </van-grid>
    <!-- /频道推荐 -->
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannel,
  deleteUserChannel,
} from '@/api/article.js';
import { mapState } from 'vuex';
import { setItem } from '@/plugins/storage.js';

export default {
  name: 'ChannelEdit',
  data() {
    return {
      allChannels: [],
      isEdit: false,
      fiexChannels: [0, 13],
    };
  },
  props: {
    myChannels: {
      type: Array,
      required: true,
    },
    active: {
      type: Number,
      required: true,
    },
  },
  computed: {
    ...mapState(['user']),
    recommendChannels() {
      return this.allChannels.filter((v) => {
        return !this.myChannels.find((v1) => {
          return v1.id === v.id;
        });
      });

      // const channels = [];
      // this.allChannels.forEach((v) => {
      //   const ret = this.myChannels.find((v1) => {
      //     return v.id === v1.id;
      //   });
      //   if (!ret) {
      //     channels.push(v);
      //   }
      // });
      // return channels;
    },
  },
  methods: {
    async deleteChannel(id) {
      if (this.user) {
        try {
          await deleteUserChannel(id);
          this.$toast.success('删除成功');
        } catch (err) {}
      } else {
        setItem('TOUTIAO_CHANNELs', this.myChannels);
        this.$toast.success('删除成功');
      }
    },
    onMyChannelClick(v, i) {
      if (this.isEdit) {
        if (this.fiexChannels.includes(v.id)) {
          return;
        }
        this.myChannels.splice(i, 1);

        if (i <= this.active) {
          this.$emit('update-active', this.active - 1, true);
        }

        this.deleteChannel(v.id);
      } else {
        this.$emit('update-active', i, false);
      }
    },
    onAdddChannel(value) {
      this.myChannels.push(value);

      if (this.user) {
        const arr = [];
        this.myChannels.forEach((v, i) => {
          if (i !== 0) {
            arr.push({
              id: v.id,
              seq: i,
            });
          }
        });
        addUserChannel(arr)
          .then((res) => {
            // console.log(res);
          })
          .catch(() => {});
      } else {
        setItem('TOUTIAO_CHANNELs', this.myChannels);
      }
    },
    async loadAllChannels() {
      try {
        const { data } = await getAllChannels();
        this.allChannels = data.data.channels;
      } catch (err) {
        this.$toast.fail('数据获取失败');
      }
    },
  },
  created() {
    this.loadAllChannels();
  },
};
</script>

<style lang="less" scoped>
.channel-edit {
  padding: 80px 0;
  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      border-radius: 10px;
      background-color: #f4f5f6;
      white-space: nowrap;
    }
    .van-grid-item__content--surround::after {
      border: 0;
    }
    .van-grid-item__text,
    .text {
      font-size: 28px;
      color: #222;
      margin-top: 0;
    }
    .active {
      color: red;
    }
    .van-grid-item__icon-wrapper {
      position: unset;
    }
  }
  /deep/ .recommend-grid {
    .van-grid-item__content {
      flex-direction: row;
      .van-icon-plus {
        font-size: 28px;
        margin-right: 6px;
      }
    }
  }
  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }
}
</style>
